﻿
using System;
using System.Linq;
using System.Collections.Generic;

using Gola.VN.Scb.Web.Common.Entities;

namespace Gola.VN.Scb.Web.Common.Models
{
    public class UIProductModels
    {
        public bool IsFilterByPriceRange { set; get; }

        public bool IsFilterByProvider { set; get; }

        public int CurrentCategoryId { set; get; }

        public int OldMaxPrice { set; get; }

        public int OldMinPrice { set; get; }

        public int SelectedMaxPrice { set; get; }

        public int SelectedMinPrice { set; get; }

        public int SelectedProviderId { set; get; }

        public IEnumerable<UICategoryModels> Categories { set; get; }

        /// <summary>
        /// This is all products that was filtered and paginated
        /// </summary>
        public PaginatedList<ProductEntity> ProductsByCategory { set; get; }

        /// <summary>
        /// Similar Product
        /// </summary>
        public List<ProductEntity> ProductsSimilar { set; get; }

        /// <summary>
        /// Store all products that was filtered by somthing
        /// </summary>
        public List<ProductEntity> Products { get; set; }

        public ProductEntity Product { get; set; }

        public ProviderEntity Provider { get; set; }

        public IEnumerable<ProviderEntity> ProviderLocations { set; get; }

        public FilterModels FilterData
        {
            get
            {
                return new FilterModels
                           {
                               ProductCount = Products.Count,
                               Provider = GetProviders(),
                               SelectedProviderId = SelectedProviderId,
                               CategoryId = GetCategoryId(),
                               MinPrice = IsFilterByPriceRange ? OldMinPrice : (int)Math.Ceiling(GetMinPrice() / 1000),
                               MaxPrice = IsFilterByPriceRange ? OldMaxPrice : (int)Math.Ceiling(GetMaxPrice() / 1000),
                               CurrentPriceLeft = IsFilterByPriceRange ? SelectedMinPrice : (int)Math.Ceiling(GetMinPrice() / 1000),
                               CurrentPriceRight = IsFilterByPriceRange ? SelectedMaxPrice : (int)Math.Ceiling(GetMaxPrice() / 1000),
                               IsFilterByProvider = IsFilterByProvider
                           };
            }

        }

        private List<ProviderEntity> GetProviders()
        {
            List<ProviderEntity> result = new List<ProviderEntity>();
            if (IsFilterByProvider)
            {
                // result.Add(ProviderService.GetProvider(SelectedProviderId));//todo: must be replace
            }
            else
            {

                var provider = Products.GroupBy(item => item.ProviderId).ToList();

                //foreach (var p in provider) //todo: must be replace
                //{
                //    result.Add(ProviderService.GetProvider(p.Key));
                //}
            }
            return result;
        }

        private int GetCategoryId()
        {
            if (Products.Count > 0)
            {
                return Products[0].CategoryId;
            }
            if (ProductsByCategory.Count > 0)
            {
                return ProductsByCategory[0].CategoryId;
            }
            return -1;
        }

        public double GetMinPrice()
        {
            if (Products == null || Products.Count == 0)
            {
                return 0;
            }
            var minPrice = (from p in Products where p.LastPrice >0
                            select p).Min(q => q.LastPrice);

            return minPrice;
        }
        public double GetMaxPrice()
        {
            if (Products == null || Products.Count == 0)
            {
                return 0;
            }
            var maxPrice = (from p in Products
                            select p).Max(q => q.LastPrice);
            return maxPrice;
        }
    }

    public class FilterModels
    {
        public bool IsFilterByProvider { set; get; }

        public int ProductCount { set; get; }

        public int MinPrice { set; get; }

        public int MaxPrice { set; get; }

        public int CurrentPriceLeft { set; get; }

        public int CurrentPriceRight { set; get; }

        public List<ProviderEntity> Provider { set; get; }

        public int CategoryId { set; get; }

        public int SelectedProviderId { set; get; }

    }
}